//import { Promise } from 'es6-promise'
import store from "../store/index"
import request from '../utils/request'
import Vue from 'vue'
import routerss from '../router/index'
import router from '../router'

import { Message, MessageBox } from 'element-ui'

$.extend($.ajaxSettings, {
            headers: {
                'sessionId':window.localStorage.getItem("token")|| ''
            }
        })
const errorCodeHandle = (resp,reject,nullTip)=>{
    if(resp.code){
        if(resp.code == 204){
            Message({
                message:resp.msg,
                type:'error'
            })
            reject(new Error(resp.msg))
            return true
        }
        else if(resp.code == 401 || resp.code == 23003 ){
            /*Message({
                message:resp.msg,
                type:'error'
            })*/
            window.localStorage.setItem("islogin",false)
             window.location.reload()
            store.dispatch("needLogin",true)
            return
            reject(new Error(resp.msg))
            return true
        }
        store.dispatch("needLogin",fasle)
        Message({
            message:resp.msg,
            type:'error'
        })
        reject(new Error(resp.msg))
        return true
    }
    if(nullTip){
        if(!resp.data){
            Message( {
                message:resp.msg,
                type:'error'
            })
            reject(new Error(resp.msg))
            return
        }
    }
    return false
}
/*$.extend($.ajaxSettings, {
            headers: {
                'sessionId': || ''
            }
        })*/
// errorRequestHandle
const errorRequestHandle = (text,reject)=>{
    Message({
        message: text,
        type: 'error',
        duration: 5 * 1000
      })
    reject(new Error(text));
}

//console.log(Vue)
const convertRequestURL = function(url,environment) {
    let converted_url
    
    let channel = process.env.CHANNEL
    //http://47.97.204.172:9026/   //loanadminapi.51yzqb.com/
    //converted_url="http://47.97.204.172:9028/"+url
    if(process.env.NODE_ENV=="development"){
        converted_url="http://47.97.204.172:9028/"+url
    }else if(process.env.NODE_ENV=="production"){
        converted_url="//tbgjadminapi.9ijie.com/"+url
    }
    return converted_url
}

const dashBoardApi = {
    
    statistics(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("loan/data/statistics"),
                data:JSON.stringify(params),
                contentType:"application/json",
                headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                dataType:"json",
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp.data
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求贷款超市数据失败',reject)
                }
            })
        })
    },
    exportFile(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("loan/data/export"),
                data:JSON.stringify(params),
                headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                dataType:"json",
                headers: {
                    'Content-Type': 'application/json; charset=utf-8'
                },
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp.data
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求导出失败',reject)
                }
            })
        })
    },
}
const loanManagementApi={
    manageProductList(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("manage/product/list"),
                headers: {
                    'sessionId':window.localStorage.getItem("token")|| '',
                },
                data:JSON.stringify(params),
                dataType:"json",
                contentType:"application/json",
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求贷款超市列表失败',reject)
                }
            })
        })
    },
    manageProductUploadIcon(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("manage/product/uploadIcon"),
                headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                data:JSON.stringify(params),
                //dataType:"json",
                contentType: "multipart/form-data",
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求贷款超市列表失败',reject)
                }
            })
        })
    },
    manageProductAdd(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("manage/product/add"),
                data:JSON.stringify(params),
                headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                dataType:"json",
                contentType: 'application/json',
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求添加商品失败',reject)
                }
            })
        })
    },
     manageProductDelete(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"get",
                url: convertRequestURL("manage/product/delete/"+params),
                headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                //data:JSON.stringify(params),
                dataType:"json",
                contentType: 'application/json',
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求删除商品失败',reject)
                }
            })
        })
    },
    manageProductUpdata(params){
        return new Promise(function(resolve, reject){
            $.ajax({
                type:"post",
                url: convertRequestURL("manage/product/update"),
                data:JSON.stringify(params),
                dataType:"json",
                contentType: 'application/json',
                headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                success: (resp) => {
                    let isError = errorCodeHandle(resp,reject)
                    if(isError) return

                    let data = resp
                    resolve(data)
                },
                error(){
                    errorRequestHandle('请求修改商品失败',reject)
                }
            })
        })
    },
}
const loginApi={
        userLogin(params){
            return new Promise(function(resolve, reject){
                $.ajax({
                    type:"post",
                    url: convertRequestURL("login"),
                    data:params,
                    headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                    },
                    dataType:"json",
                    success: (resp) => {
                        let isError = errorCodeHandle(resp,reject)
                        if(isError) return
                        let data = resp.data
                        resolve(data)
                    },
                    error(){
                        errorRequestHandle('登录失败',reject)
                    }
                });
            })
        },
        loginOut(params){
            return new Promise(function(resolve, reject){
                $.ajax({
                    type:"post",
                    url: convertRequestURL("loginOut"),
                    headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                    //data:params,
                    dataType:"json",
                    success: (resp) => {
                        let isError = errorCodeHandle(resp,reject)
                        if(isError) return

                        let data = resp.data
                        resolve(data)
                    },
                    error(){
                        errorRequestHandle('请求登出失败',reject)
                    }
                });
            })
        },
        resetPwd(params){
            return new Promise(function(resolve, reject){
                $.ajax({
                    type:"post",
                    url: convertRequestURL("resetPwd"),
                    headers: {
                    'sessionId':window.localStorage.getItem("token")|| ''
                },
                    data:params,
                    dataType:"json",
                    success: (resp) => {
                        let isError = errorCodeHandle(resp,reject)
                        if(isError) return

                        let data = resp.data
                        resolve(data)
                    },
                    error(){
                        errorRequestHandle('请修改密码失败',reject)
                    }
                });
            })
        },
}
export {
    dashBoardApi,
    loanManagementApi,
    convertRequestURL,
    loginApi
}